package com.lfg.leetcode.leetcode100;

import com.lfg.leetcode.leetcode0.TreeNode;

import java.util.ArrayList;
import java.util.List;


public class No102levelOrder {
    public static void main(String[] args) {
        List<List<Integer>> ret = levelOrder(TreeNode.stringToTreeNode("[3,9,20,null,null,15,7]"));
        System.out.println("[" + ret + "]");
    }

    public static List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> ret = new ArrayList<>(100);
        if (root == null) {
            return ret;
        }
        List<TreeNode> nodes = new ArrayList<>(100);
        nodes.add(root);
        while (!nodes.isEmpty()) {
            List<TreeNode> tmp = new ArrayList<>(100);
            List<Integer> data = new ArrayList<>(100);
            for (TreeNode node : nodes) {
                if (node.left != null) {
                    tmp.add(node.left);
                }
                if (node.right != null) {
                    tmp.add(node.right);
                }
                data.add(node.val);
            }
            nodes = tmp;
            ret.add(data);
        }
        return ret;
    }

}
